/**
 * Title:        流程穿透检测
 * Company:      泛微软件
 */


package checkWithCondit;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.workflow.workflow.WFModeNodeFieldManager;



public class RequestPenetrateCheck extends BaseBean implements checkWithConBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        List resultList = new ArrayList();
        String result = "";
        RecordSet RecordSet1 = new RecordSet();
        (new BaseBean()).writeLog("RequestPenetrateCheck  start");
        if(dataMap.get("requestid") != null && !dataMap.get("requestid").equals("")){
            String workflowtype="";

            String requestid = Util.null2String(dataMap.get("requestid"));
            String nodeid = Util.null2String(dataMap.get("nodeid"));
            String workflowid = Util.null2String(dataMap.get("workflowid"));
            ArrayList<String> nodeList = new ArrayList<String>();
            int isbill = -1;
            int formid = -1;
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            RecordSet rs2 = new RecordSet();
            BaseBean bb = new BaseBean();

            String returnStringProp="";
            String returnStringNode="";
            String returnStringType="";

            String nextnodetype="";
            String nextnodeattribute="";

          //基于流程获取节点
          if(!requestid.equals("")&&nodeid.equals("")){
              //取流程数据
              //查找workflowid,workflowtype,所有节点
              String sql = "select t.workflowid, t1.workflowtype,t1.isbill,t1.formid from workflow_requestbase t, workflow_base t1 where t.workflowid=t1.id and t.requestid="+requestid;
              rs.executeSql(sql);
              if(rs.next()){
                  workflowid=Util.null2String(rs.getString("workflowid"));
                  workflowtype = Util.null2String(rs.getString("workflowtype"));
                  isbill = rs.getInt("isbill");
                  formid = rs.getInt("formid");
              }else{
                  result = resultNeedFix;
                  Map resultMap = new HashMap<String,String>();
                  resultMap.put("showMessage","找不到输入流程");

                  resultList.add(resultMap);
              }
              //取节点数据
              //--查找到所有的节点及该节点之后的节点
              sql = "select workflow_nodebase.id,workflow_nodebase.nodename  from workflow_flownode, workflow_nodebase where (workflow_nodebase.IsFreeNode is null or workflow_nodebase.IsFreeNode != '1')"+
                    "and workflow_nodebase.id = workflow_flownode.nodeid   and workflowid = "+workflowid+" order by workflow_flownode.nodetype, workflow_nodebase.id";
              //out.println(sql);
              rs.executeSql(sql);
              boolean after = false;
              while(rs.next()){
                  String tmpnodeid=Util.null2String(rs.getString("id"));
                  String tmpnodename=Util.null2String(rs.getString("nodename"));
                  nodeList.add(tmpnodeid+","+tmpnodename);    
              }

          }
          if(!requestid.equals("")&&!workflowid.equals("")&&formid!=-1&&isbill!=-1){
              //1 检查配置文件
              try {
                      //不穿透的流程ID
                      String notPenetratewfids = Util.null2String(bb.getPropValue("workflowNotPenetrate", "workflowids"));
                      //不穿透的系统表单ID
                      String notPenetratesysformids = Util.null2String(bb.getPropValue("workflowNotPenetrate", "sysformids"));
                      //不穿透的自定义表单ID
                      String notPenetrateformids = Util.null2String(bb.getPropValue("workflowNotPenetrate", "formids"));
                      //在配置文件中找到对应的ID则不穿透
                      if(("," + notPenetratewfids + ",").indexOf("," + workflowid + ",") != -1){
                          result = resultNeedFix;
                          Map resultMap = new HashMap<String,String>();
                          resultMap.put("showMessage","在配置文件中找到对应的ID则不穿透");
                          resultList.add(resultMap);
                      }
                      //在配置文件中找到对应的系统表单ID则不穿透
                      if(isbill == 1 && formid > 0 && ("," + notPenetratesysformids + ",").indexOf("," + formid + ",") != -1){
                          result = resultNeedFix;
                          Map resultMap = new HashMap<String,String>();
                          resultMap.put("showMessage","在配置文件中找到对应的系统表单ID则不穿透");

                          resultList.add(resultMap);
                      } else if (((isbill == 1 && formid < 0) || (isbill != 1 && formid > 0)) 
                              && ("," + notPenetrateformids + ",").indexOf("," + formid + ",") != -1){
                          result = resultNeedFix;
                          Map resultMap = new HashMap<String,String>();
                          resultMap.put("showMessage","在配置文件中找到对应的系统表单ID则不穿透");

                          resultList.add(resultMap);
                      }
                  } catch (Exception e) {
                  }
              
              //2 检查节点是否可编辑
                 
                  String sql1 = "";
                  
                  WFModeNodeFieldManager wFModeNodeFieldManager=new WFModeNodeFieldManager();

                  boolean isMode=wFModeNodeFieldManager.getIsModeByWorkflowIdAndNodeId(Util.getIntValue(workflowid),Util.getIntValue(nodeid));

                  if(isMode){
                      rs.executeSql("select count(nodeid) from workflow_modeview where isedit = '1' and nodeid = " + nodeid+" and formid="+formid);

                      sql1 ="select bf.fieldname,bf.id from workflow_modeview mv,workflow_billfield bf where mv.fieldid=bf.id and nodeid="+ nodeid+" and formid="+formid;

                  }else{
                      rs.executeSql("select count(nodeid) from workflow_nodeform where isedit = '1' and nodeid = " + nodeid);

                      sql1 = "select bf.fieldname,bf.id from workflow_nodeform mv,workflow_billfield bf where mv.fieldid=bf.id and nodeid="+nodeid;
                  }
                  if (rs.next()) {
                      if (Util.getIntValue(rs.getString(1), 0) > 0){

                          returnStringNode = "当前节点有可编辑项 ：";

                          rs1.executeSql(sql1);
                          while(rs1.next()){
                              returnStringNode +=(rs1.getString("fieldname")+"-");
                          }               
                      } 
                  }

                  if(returnStringNode.lastIndexOf("-")>0) returnStringNode = returnStringNode.substring(0,returnStringNode.length()-1);
                  if(!returnStringNode.equals("")){
                      result = resultNeedFix;
                      Map resultMap = new HashMap<String,String>();
                      resultMap.put("showMessage","在配置文件中找到对应的系统表单ID则不穿透");

                      resultList.add(resultMap);
                  }

              //3 检查节点类型
                  rs2.executeSql("SELECT nb.nodeattribute,fn.nodetype,nb.passnum FROM workflow_nodebase nb,workflow_flownode fn WHERE nb.id = fn.nodeid AND fn.nodeid="+nodeid);
                  if(rs2.next()){
                       nextnodetype = Util.null2String(rs2.getString("nodetype"));
                       nextnodeattribute = Util.null2String(rs2.getString("nodeattribute"));
                  }

                  if(!nextnodetype.equals("1")){
                      result = resultNeedFix;
                      Map resultMap = new HashMap<String,String>();
                      resultMap.put("showMessage","当前节点类型不是 批准");

                      resultList.add(resultMap);
                  }
                  if(nextnodeattribute.equals("1") || nextnodeattribute.equals("3") || nextnodeattribute.equals("4")){
                      result = resultNeedFix;
                      Map resultMap = new HashMap<String,String>();
                      resultMap.put("showMessage","分叉起始节点、合并点不作跳过");

                      resultList.add(resultMap);
                  }
                          
              
          }
        }else{
            result = resultERROR;
            resultList.add("请输入requestid");
        }
        (new BaseBean()).writeLog("RequestPenetrateCheck  end");
        if(resultList.size() == 0){
            result = resultOk;
            resultList.add("流程可以穿透");
        }
        return formatReturnJsonCheck(result, resultList, this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {

        //重新加载DIV
        needReload = "1";
        return checkData(new HashMap());
    }

    @Override
    public String getClassName() {
        return "流程穿透检测";
    }

    @Override
    public String getClassDesc() {
        return "指定流程节点不能穿透（检查方向：穿透节点满足条件问题。）";
    }

    @Override
    public String getPopupUrl() {
        return "/flowtoolbar/checkWithCondit/RequestPenetrateCheck.jsp";
    }

}



